package com.chucang.shucang.oms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chucang.shucang.common.base.constant.SecurityConstant;
import com.chucang.shucang.common.base.exception.FastException;
import com.chucang.shucang.common.base.utils.OpsUtil;
import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.common.mq.to.CollectionOrderBaseTO;
import com.chucang.shucang.common.security.annotation.Inner;
import com.chucang.shucang.oms.constant.OmsCommonConstant;
import com.chucang.shucang.oms.entity.CollectionOrderBaseEntity;
import com.chucang.shucang.oms.exception.OmsErrorCode;
import com.chucang.shucang.oms.feign.PmsService;
import com.chucang.shucang.oms.feign.vo.CollectionBaseEntity;
import com.chucang.shucang.oms.service.CollectionOrderBaseService;
import com.chucang.shucang.oms.vo.CollectionOrderBaseResVO;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 藏品基础订单表
 *
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-09-12
 */
@RestController
@RequestMapping("/collectionorderbase")
@RequiredArgsConstructor
@Api(tags = "藏品基础订单表")
public class CollectionOrderBaseController {

    private final CollectionOrderBaseService collectionOrderBaseService;
    private final PmsService pmsService;

    /**
     * 确认订单信息
     *
     * @param orderSn
     * @return
     */
    @Inner(value = false)
    @GetMapping("/checkCollectionOrder/{orderSn}")
    public R<CollectionOrderBaseResVO> checkCollectionOrder(@PathVariable String orderSn) {
        LambdaQueryWrapper<CollectionOrderBaseEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CollectionOrderBaseEntity::getOrderSn, orderSn)
                .select();
        CollectionOrderBaseEntity one = collectionOrderBaseService.getOne(lambdaQueryWrapper);
        CollectionOrderBaseResVO collectionOrderBaseResVO = new CollectionOrderBaseResVO();
        BeanUtil.copyProperties(one, collectionOrderBaseResVO);
        //藏品信息
        R<CollectionBaseEntity> baseInfo = pmsService.getCollectionBaseInfo(one.getCollectionId(), SecurityConstant.FROM_IN);
        CollectionBaseEntity baseEntity = OpsUtil.of(baseInfo)
                .assertDataNotNull(e -> new FastException("查询藏品信息错误！"))
                .getData()
                .get();
        collectionOrderBaseResVO.setCoverPicture(baseEntity.getCoverPicture());
        collectionOrderBaseResVO.setDcName(baseEntity.getDcName());
        return R.ok(collectionOrderBaseResVO);
    }

    /**
     * 取消订单
     *
     * @param orderSn
     * @return
     */
    @GetMapping("/cancelCollectionOrder/{orderSn}")
    public R<String> cancelCollectionOrder(@PathVariable String orderSn) {
        if (!collectionOrderBaseService.cancelCollectionOrder(orderSn)) {
            return R.failed(OmsErrorCode.ORDER_CAN_NOT_CANCEL.getCode(), OmsErrorCode.ORDER_CAN_NOT_CANCEL.getMsg());
        }
        return R.ok();
    }

    @Inner
    @PostMapping("/createCollectionOrder")
    public R<String> createCollectionOrder(@RequestBody CollectionOrderBaseTO collectionOrderBase) {
        CollectionOrderBaseEntity orderBaseEntity = new CollectionOrderBaseEntity();
        BeanUtil.copyProperties(collectionOrderBase, orderBaseEntity);
        orderBaseEntity.setStatus(OmsCommonConstant.WAITING_FOR_PAY);
        collectionOrderBaseService.save(orderBaseEntity);
        return R.ok();
    }

    @GetMapping("/cancelCollectionNormalOrder/{orderSn}")
    public R<String> cancelCollectionNormalOrder(@PathVariable String orderSn) {
        if (!collectionOrderBaseService.cancelCollectionNormalOrder(orderSn)) {
            return R.failed(OmsErrorCode.ORDER_CAN_NOT_CANCEL.getCode(), OmsErrorCode.ORDER_CAN_NOT_CANCEL.getMsg());
        }
        return R.ok();
    }

    @Inner
    @GetMapping("/collectionOrderSuccessByAliPay/{orderSn}")
    public R<String> collectionOrderSuccessByAliPay(@PathVariable String orderSn) {
        LambdaUpdateWrapper<CollectionOrderBaseEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CollectionOrderBaseEntity::getOrderSn, orderSn)
                .set(CollectionOrderBaseEntity::getStatus, 3)
                .set(CollectionOrderBaseEntity::getPayType, 1);
        collectionOrderBaseService.update(updateWrapper);
        return R.ok();
    }

    @Inner
    @GetMapping("/collectionOrderSuccessByWallet/{orderSn}")
    public R<String> collectionOrderSuccessByWallet(@PathVariable String orderSn) {
        LambdaUpdateWrapper<CollectionOrderBaseEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CollectionOrderBaseEntity::getOrderSn, orderSn)
                .set(CollectionOrderBaseEntity::getStatus, 3)
                .set(CollectionOrderBaseEntity::getPayType, 0);
        collectionOrderBaseService.update(updateWrapper);
        return R.ok();
    }

}